/*
 * @(#)DefaultTreeCellRenderer.java	1.51 04/01/23
 *
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.afaker.rss.gui.tree;

import com.afaker.rss.context.ResourceContext;
import com.afaker.rss.gui.action.MenuAction;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.Border;
import javax.swing.tree.DefaultTreeCellRenderer;
import org.jdom.Element;

public class RSSTreeCellRenderer extends DefaultTreeCellRenderer {

    /** Last tree the renderer was painted in. */
    private JTree tree;
    private ImageIcon icon = new ImageIcon(
            ResourceContext.getResourceContext().getProperty("feed.icon"));
    private ImageIcon folderIcon = new ImageIcon(
            ResourceContext.getResourceContext().getProperty("feed.folder.icon"));
    private String tipText;
    private Border border1;
    private Border border2;

    /**
     * Returns a new instance of DefaultTreeCellRenderer.  Alignment is
     * set to left aligned. Icons and text color are determined from the
     * UIManager.
     */
    public RSSTreeCellRenderer() {
        super();
        border1 = BorderFactory.createEmptyBorder(2, 0, 2, 0);
        border2 = BorderFactory.createEmptyBorder();
    }

    /**
     * Configures the renderer based on the passed in components.
     * The value is set from messaging the tree with
     * <code>convertValueToText</code>, which ultimately invokes
     * <code>toString</code> on <code>value</code>.
     * The foreground color is set based on the selection and the icon
     * is set based on on leaf and expanded.
     */
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean sel,
            boolean expanded,
            boolean leaf, int row,
            boolean hasFocus) {
        String stringValue = tree.convertValueToText(value, sel,
                expanded, leaf, row, hasFocus);

        this.tree = tree;
        this.tree.putClientProperty("JTree.lineStyle", "Angled");
        this.hasFocus = hasFocus;
        setText(stringValue);
        if (sel) {
            setForeground(getTextSelectionColor());
        } else {
            setForeground(getTextNonSelectionColor());
        }
        if (value instanceof RSSTreeNode) {
            RSSTreeNode node = (RSSTreeNode) value;
            if ((node.getUserObject()) instanceof Icon) {
                setIcon((Icon) node.getUserObject());
            } else if ((node.getUserObject()) instanceof Element) {
                Element ele = (Element) node.getUserObject();
                if ((ele.getName().equals(MenuAction.CHANNEL_NODE_NAME))) {
                    node.gainStatistics();
                    int unRead = node.getUnReadedEntryNumber();
                    setText(ele.getAttributeValue(MenuAction.TITLE_ATTRIBUTE_NAME) +
                            "(" + unRead + "/" + node.getAllEntriesNumber() + ")");
                    tipText = ele.getAttribute(MenuAction.DESCRIPTION_ATTRIBUTE_NAME).getValue();
                    setIcon(icon);
                    setBorder(border1);
                } else {
                    setIcon(folderIcon);
                    setBorder(border2);
                }
            } else {
                if (leaf) {
                    setIcon(getLeafIcon());
                } else if (expanded) {
                    setIcon(getOpenIcon());
                } else {
                    setIcon(getClosedIcon());
                }
            }
        }
        setComponentOrientation(tree.getComponentOrientation());
        selected = sel;
        return this;
    }

    public String getToolTipText() {
        return tipText;
    }
}